home *** CD-ROM | disk | FTP | other *** search
/ Nejlepší hry / Nejlepsi hry.iso / hry / plane arcade / planearcade.exe / tank3.bmp / sound.cpp < prev    next >
C/C++ Source or Header  |  2004-08-23  |  8KB  |  392 lines

  1.  
  2.  
  3. #include "main.h"
  4.  
  5. //-----------
  6. //Global
  7. //-----------
  8. ALCcontext *Context = NULL;
  9. ALCdevice  *Device = NULL;
  10.  
  11.  
  12. //------------------------------------------------------------------
  13. // Name: InitializeSound()
  14. // Desc: Inicializacia zvuku
  15. //------------------------------------------------------------------
  16. void InitializeSound()
  17. {
  18.  
  19.     //ak je vypnuty zvuk skonci
  20.     if (SoundEnabled == 0)
  21.         return;
  22.  
  23.  
  24.     //OpenAL3D
  25.     //--------------
  26.     LogPrint("Inicializujem OpenAL3D");
  27.  
  28.     //Open device
  29.     if(SoundEnabled3D == 1)
  30.     {
  31.         if(HardwareSound == 1)
  32.             Device = alcOpenDevice((ALubyte*)"DirectSound3D");
  33.         else
  34.             Device = alcOpenDevice((ALubyte*)"DirectSound");
  35.     }
  36.     else
  37.         Device = alcOpenDevice((ALubyte*)"DirectSound");
  38.  
  39.  
  40.     if (Device == NULL)
  41.     {
  42.         //we failed to initialize the device
  43.         LogPrint(" chyba pri inicializacii");
  44.     }
  45.  
  46.     //Create context(s)
  47.     Context=alcCreateContext(Device,NULL);
  48.     //Set active context
  49.     alcMakeContextCurrent(Context);
  50.  
  51.     //alutInit(0,NULL);
  52.  
  53.  
  54.  
  55. }
  56.  
  57. //------------------------------------------------------------------
  58. // Name: CleanUpSound()
  59. // Desc: Odstrani z pamati objekty zvuku
  60. //------------------------------------------------------------------
  61. void CleanUpSound()
  62. {
  63.     //ak je vypnuty zvuk skonci
  64.     if (SoundEnabled == 0)
  65.         return;
  66.  
  67.     //OpenAL3D
  68.     //--------------
  69.  
  70.     //Get active context
  71.     Context=alcGetCurrentContext();
  72.     //Get device for active context
  73.     Device=alcGetContextsDevice(Context);
  74.     //Disable context
  75.     alcMakeContextCurrent(NULL);
  76.     //Release context(s)
  77.     alcDestroyContext(Context);
  78.     //Close device
  79.     alcCloseDevice(Device);
  80.  
  81. }
  82.  
  83.  
  84.  
  85. //------------------------------------------------------------------
  86. // Name: SOUND()
  87. // Desc: Konstruktor
  88. //------------------------------------------------------------------
  89. SOUND::SOUND()
  90. {    
  91.  
  92.     //MP3
  93.     m_pGraph = NULL;
  94.     m_pMediaControl = NULL;
  95.     m_pMediaPosition = NULL;
  96.  
  97.  
  98. }
  99.  
  100. //------------------------------------------------------------------
  101. // Name: ~SOUND()
  102. // Desc: Destruktor
  103. //------------------------------------------------------------------
  104. SOUND::~SOUND()
  105. {
  106.     Stop();
  107.  
  108.     //Direct Music
  109.     //-------------
  110.     if (m_enumFormat == MP3)
  111.     {
  112.         if(m_pGraph)
  113.             m_pGraph->Release();
  114.         if(m_pMediaControl)
  115.             m_pMediaControl->Release();
  116.         if(m_pMediaPosition)
  117.             m_pMediaPosition->Release();
  118.  
  119.         //Uninitilize COM
  120.         CoUninitialize();
  121.     }
  122.  
  123.     //OpenAL3D
  124.     //-----------
  125.     if (m_enumFormat == WavMidi3D)
  126.     {
  127.         alDeleteSources(1,&alSource);
  128.         alDeleteBuffers(1,&alSampleSet);
  129.     }
  130.  
  131.     
  132. }
  133.  
  134.  
  135. //------------------------------------------------------------------
  136. // Name: InitialiseForWavMidi()
  137. // Desc: pripravy zvuk pre prehrßvanie MIDI alebo WAW
  138. //------------------------------------------------------------------
  139. void SOUND::InitialiseForWavMidi()
  140. {
  141.  
  142.     //ak je vypnuty zvuk skonci
  143.     if (SoundEnabled == 0)
  144.         return;
  145.  
  146.     LogPrint("Vytvaram zvuk WavMidi");
  147.  
  148.     m_enumFormat = WavMidi3D;
  149. }
  150.  
  151. //------------------------------------------------------------------
  152. // Name: InitialiseForWavMidi()
  153. // Desc: pripravy zvuk pre prehrßvanie MIDI alebo WAW
  154. //------------------------------------------------------------------
  155. void SOUND::InitialiseForWavMidi3D()
  156. {
  157.     //ak je vypnuty zvuk skonci
  158.     if (SoundEnabled == 0)
  159.         return;
  160.  
  161.     LogPrint("Vytvaram zvuk WavMidi3D");
  162.  
  163.     m_enumFormat = WavMidi3D;
  164.  
  165.     alSourcei(alSource,AL_SOURCE_RELATIVE,AL_TRUE);
  166.  
  167. }
  168.  
  169. //------------------------------------------------------------------
  170. // Name: InitialiseForMP3()
  171. // Desc: pripravy zvuk na prehravanie MP3
  172. //------------------------------------------------------------------
  173. void SOUND::InitialiseForMP3()
  174. {
  175.     //ak je vypnuty zvuk skonci
  176.     if (SoundEnabled == 0)
  177.         return;
  178.  
  179.     LogPrint("Inicializujem Direct Music");
  180.  
  181.     //Initialise COM
  182.     CoInitialize(NULL); 
  183.  
  184.     LogPrint("Vytvaram zvuk MP3");
  185.  
  186.     CoCreateInstance(CLSID_FilterGraph, NULL,
  187.                      CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);
  188.  
  189.     m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);
  190.  
  191.     m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
  192.  
  193.     m_enumFormat = MP3;
  194. }
  195.  
  196.  
  197.  
  198. //------------------------------------------------------------------
  199. // Name: LoadSound()
  200. // Desc: Loadne zvuk
  201. //------------------------------------------------------------------
  202. bool SOUND::LoadSound(char* szSoundFileName)
  203. {
  204.     //ak je vypnuty zvuk skonci
  205.     if (SoundEnabled == 0)
  206.         return false;
  207.  
  208.     WCHAR wstrSoundPath[MAX_PATH];
  209.  
  210.     //Log Info
  211.     char cBuf[80];
  212.     sprintf(cBuf,"  Zvuk: %s",szSoundFileName);
  213.     LogPrint(cBuf);
  214.  
  215.     switch(m_enumFormat)
  216.     {
  217.  
  218.         //MP3
  219.         //-------------
  220.         case MP3:
  221.             //Convert the path to unicode.
  222.             MultiByteToWideChar(CP_ACP, 0, szSoundFileName, -1, wstrSoundPath, MAX_PATH);
  223.  
  224.             m_pGraph->RenderFile(wstrSoundPath, NULL);
  225.         break;
  226.  
  227.         //Wav3D
  228.         //-------------
  229.         case WavMidi3D:
  230.  
  231.             //load our sound
  232.             ALboolean    loop;
  233.             alutLoadWAVFile(szSoundFileName,&alFormatBuffer, (void **) &alBuffer,(unsigned int *)&alBufferLen, &alFreqBuffer, &loop);
  234.  
  235.             alGenSources(1, &alSource);
  236.             alGenBuffers(1, &alSampleSet);
  237.             alBufferData(alSampleSet, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);
  238.             alSourcei(alSource, AL_BUFFER, alSampleSet);
  239.  
  240.             alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);            
  241.  
  242.             //set the pitch
  243.             alSourcef(alSource,AL_PITCH,1.0f);
  244.             //set the gain
  245.             alSourcef(alSource,AL_GAIN,1.0f);
  246.             
  247.             if (alSource == NULL)
  248.                 LogPrint("  chyba pri vytvarani zvuku");
  249.             
  250.         break;
  251.  
  252.         //Wav2D
  253.         //------
  254.         case WavMidi:
  255.  
  256.             
  257.  
  258.         break;
  259.  
  260.         default:
  261.             return false;
  262.     }
  263.  
  264.     return true;
  265. }
  266.  
  267. //------------------------------------------------------------------
  268. // Name: Play()
  269. // Desc: Prehra zvuk - dwNumOfRepeats - pocet prehrani
  270. //------------------------------------------------------------------
  271. bool SOUND::Play(bool Loop)
  272. {
  273.     //ak je vypnuty zvuk skonci
  274.     if (SoundEnabled == 0)
  275.         return false;
  276.     
  277.     switch(m_enumFormat)
  278.     {
  279.         //MP3
  280.         //-------------
  281.         case MP3:
  282.             
  283.             //rewind
  284.             m_pMediaPosition->put_CurrentPosition(0);
  285.             
  286.             //Play mp3
  287.             m_pMediaControl->Run();
  288.         break;
  289.  
  290.         //Wav3D
  291.         //-------------
  292.         case WavMidi3D:
  293.  
  294.             //set looping to true
  295.             alSourcei(alSource,AL_LOOPING,Loop);
  296.  
  297.             alSourcePlay(alSource);
  298.         break;
  299.  
  300.         //Wav2D
  301.         //-------------
  302.         case WavMidi:
  303.  
  304.                         
  305.         break;
  306.         default:
  307.             return false;
  308.     }
  309.  
  310.     return true;
  311. }
  312.  
  313. //------------------------------------------------------------------
  314. // Name: Stop()
  315. // Desc: Zastavy zvuk
  316. //------------------------------------------------------------------
  317. bool SOUND::Stop()
  318. {
  319.     //ak je vypnuty zvuk skonci
  320.     if (SoundEnabled == 0)
  321.         return false;
  322.  
  323.     switch(m_enumFormat)
  324.     {
  325.         //MP3
  326.         //-------------
  327.         case MP3:
  328.             m_pMediaControl->Stop();
  329.         break;
  330.  
  331.         //Wav3D
  332.         //-------------
  333.         case WavMidi3D:
  334.  
  335.             alSourceStop(alSource);
  336.         
  337.         break;
  338.  
  339.         //Wav2D
  340.         //-------------
  341.         case WavMidi:
  342.  
  343.         
  344.         break;
  345.  
  346.         default:
  347.             return false;
  348.     }
  349.  
  350.     return true;
  351. }
  352.  
  353. //------------------------------------------------------------------
  354. // Name: SetPosition()
  355. // Desc: Nastavi poziciu zvuku, zvuk sa prepoΦitava pod╛a pozicie kamery
  356. //------------------------------------------------------------------
  357. void SOUND::SetPosition(VECTOR3D Point,float Scale)
  358. {
  359.     //ak je vypnuty zvuk skonci
  360.     if (SoundEnabled == 0)
  361.         return;
  362.  
  363.     if (m_enumFormat != WavMidi3D)
  364.         return;
  365.  
  366.  
  367.     //Camera
  368.     //---------
  369.     alListener3f(AL_POSITION,0.0f,0.0f,0.0f);
  370.  
  371.     float vec[6];
  372.     vec[0] = 0.0f;
  373.     vec[1] = 0.0f;
  374.     vec[2] = 1.0f;
  375.     vec[3] = 0.0f;
  376.     vec[4] = 0.0f;
  377.     vec[5] = 1.0f;
  378.     alListenerfv(AL_ORIENTATION, vec);
  379.  
  380.  
  381.     //Sound
  382.     //----------
  383.     Point = TransformPoint(Point,matView);
  384.  
  385.     alSource3f(alSource,AL_POSITION,Point.X/Scale,-Point.Y/Scale,-Point.Z/Scale);
  386.     alSource3f(alSource,AL_VELOCITY,-20.0f,0.0f,20.0f);
  387.  
  388.  
  389. }
  390.  
  391.  
  392.